<?php
    /**
     * Created by PhpStorm.
     * 版权所有: 2019~2022 [ hhygyl ]
     * Date: 2019/8/14-10:38
     * Link: http://luckyadmin.luckyhhy.cn
     * FileName: Base.php
     * Keys: ctrl+alt+L/ctrl+s(代码格式化) ctrl+J(代码提示) ctrl+R(替换)ALT+INSERT(生成代码(如GET,SET方法,构造函数等) , 光标在类中才生效)
     * CTRL+ALT+O (优化导入的类和包 需要配置) SHIFT+F2(高亮错误或警告快速定位错误)
     * CTRL+SHIFT+Z(代码向前) CTRL+SHIFT+/ (块状注释) ctrl+shift+enter(智能完善代码 如if())
     **************************************************************
     *                                                            *
     *   .=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-.       *
     *    |                     ______                     |      *
     *    |                  .-"      "-.                  |      *
     *    |                 /            \                 |      *
     *    |     _          |              |          _     |      *
     *    |    ( \         |,  .-.  .-.  ,|         / )    |      *
     *    |     > "=._     | )(__/  \__)( |     _.=" <     |      *
     *    |    (_/"=._"=._ |/     /\     \| _.="_.="\_)    |      *
     *    |           "=._"(_     ^^     _)"_.="           |      *
     *    |               "=\__|IIIIII|__/="               |      *
     *    |              _.="| \IIIIII/ |"=._              |      *
     *    |    _     _.="_.="\          /"=._"=._     _    |      *
     *    |   ( \_.="_.="     `--------`     "=._"=._/ )   |      *
     *    |    > _.="                            "=._ <    |      *
     *    |   (_/                                    \_)   |      *
     *    |                                                |      *
     *    '-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='      *
     *                                                            *
     *                    JUST FIND YOUR BUG !                    *
     **************************************************************
     */


    namespace app\index\controller;

    use app\common\controller\HomeBase;
    use service\UtilService;
    use think\Cache;
    use think\Exception;
    use think\Log;
    use think\Request;

    class Base extends HomeBase
    {

        protected $article_model;
        protected $navigate_id; //栏目ID
        protected $model_data;  //获取当前model数据信息
        protected $param; //接收的参数
        protected $redis;
        protected $on_line_ip;//统计每日访问数量


        /**
         * @throws \Exception
         * @author: hhygyl <jackhhy520@qq.com>
         * @name: _initialize
         * @describe:
         */
        protected function _initialize()
        {
            parent::_initialize(); // TODO: Change the autogenerated stub

            $this->navigate_id=$this->request->param("navigate_id/d", 0 ,'intval');
            $this->assign("nav_id", $this->navigate_id); //赋值导航栏目ID

            $this->param = $this->request->param();
            $this->article_model=model("Article"); //文章模型

            $request=Request::instance();
            $this->assign("now_url",$request->url(true)); //当前文章的路径
            $this->assign("domain",$request->domain()); //当前域名

            $IP=UtilService::getip();
            $this->assign("IP",$IP); //当前ip地址

            //根据当前controller获取模型数据
            $this->model_data=model('admin/Model')->adminGetModelDataToTableName(strtolower($request->controller()));

            $this->navigate();//导航数据

            $this->ip_online();

            $this->assign("on_line_ip",$this->on_line_ip); //统计今日访问人数
        }




        /**
         * @return bool
         * @throws \Exception
         * @author: hhygyl <jackhhy520@qq.com>
         * @name: ip_online
         * @describe:统计今日访问ip
         */
        public function ip_online(){
            $redis=new \Redis();
            $ok=$redis->connect("127.0.0.1",6379);
            if($ok==false){
                Log::write($redis->getLastError()); //记录错误日志
                $this->on_line_ip=random_int(10,100);
                return true;
            }
            $ip=UtilService::getip(); //获取当前IP
            //$browse=UtilService::getBrowser(); //获取当前访问者浏览器
            $time=date("Y-m-d",time()); //时间

            $is_online=$redis->zScore($time,$ip); //判断是否在客户端
            if(empty($is_online)){
                $redis->zAdd($time,time(),$ip); //加入当前IP为客户端
            }
            //统计今日访问人数
            $this->on_line_ip=$redis->zCard($time);
        }


        /**
         * @author: hhygyl <jackhhy520@qq.com>
         * @name: navigate
         * @describe:栏目导航
         */
        public function navigate(){
            $this->assign("navigate_data",model("navigate")->indexGetNavigate()); //栏目导航
        }


        /**
         * @param array $where
         * @param int $limit
         * @param string $order
         * @author: hhygyl <jackhhy520@qq.com>
         * @name: right_article
         * @describe:右侧文章推荐
         */
        public function right_article($where=[],$limit=5,$order="create_time desc"){
            $article=$this->article_model->right_article($where,$limit,$order);
            $this->assign("right_article",$article);
        }



        /**
         * @param $table
         * @param $field
         * @param $id
         * @author: hhygyl <jackhhy520@qq.com>
         * @name: all_zan
         * @describe:各种点赞
         */
        public function all_zan($table,$field,$id){
            $ok=UtilService::getip()."_".$id."_".$table;
            if(Cache::has($ok)){
                $this->error("亲，您已经点过赞了哟！");
            }
            try{
                $res=db($table)->where("id",$id)->setInc($field);
                if($res){
                   Cache::set($ok,$ok,300);//5分钟
                    $this->success("谢谢小哥哥小姐姐的点赞哟！");
                }else{
                    $this->error("亲，点赞失败！");
                }
            }catch (Exception $exception){
                $this->error($exception->getMessage());
            }
        }


        /**
         * @param $qq
         * @return bool
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: hhygyl <jackhhy520@qq.com>
         * @name: chek_user
         * @describe:检测用户是否已经被拉黑
         */
        public function chek_user($qq){
            if(empty($qq)){
                return false;
            }
            $info=db("users")->where("qq",$qq)->find();
            if((int)$info['status']==1){
                return true;
            }else{
               $this->error("用户【".$qq."】已被站长拉黑，禁止操作！");
               exit;
            }
        }




    }